package user

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"apiserver/model"
	"apiserver/handler"
	"apiserver/errno"
)

func Update(c *gin.Context) {
	//日志

	//更新操作
	userId, _ := strconv.Atoi(c.Param("uid"))
	var u model.UserModel
	if err := c.Bind(&u); err != nil {
		handler.SendResponse(c, errno.ErrBind, nil)
		return
	}
	//验证是否有效
	u.Id = uint64(userId)
	if err := u.Validate(); err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}
	//验证密码
	if err := u.Encrypt(); err != nil {
		handler.SendResponse(c, errno.ErrEncrypt, nil)
		return
	}
	//更新用户信息
	if err := u.Update(); err != nil {
		handler.SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	handler.SendResponse(c, nil, nil)
}
